// pages/regimental-commander/after-sales-details/after-sales-details.js
import Dialog from '../../../dist/dialog/dialog'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order: null,
    active: 0,
    receivingStatusShow: false,
    receivingStatusList: [
      { name: '司机未到自提点', value: 1 },
      { name: '司机送达，该商品没货', value: 2 },
      { name: '货物在自提点，顾客未提货', value: 3 },
      { name: '顾客已提货', value: 4 },
      { name: '顾客已退货到自提点', value: 5 }
    ],
    selectedReceivingStatus: '',
    btn: {},
    refundRecordList: []
  },

  /**
   * 收货状态选中跳转到售后处理页面
   * @param {Object} e 
   */
  receivingStatusSelected: function(e) {
    const { item } = e.currentTarget.dataset
    const obj = {
      receivingStatus: item,
      order: this.data.order,
      btn: this.data.btn
    }
    const objStr = JSON.stringify(obj)
    wx.navigateTo({
      url: '/pages/regimental-commander/refund-handle/refund-handle?params=' + encodeURIComponent(objStr),
    })
  },

  /**
   * 显示动作面板
   * @param {Object} e 
   */
  showActionSheet: function(e) {
    const type = e.currentTarget.dataset.sheetType
    this.setData({
      [type]: true
    })
  },

  /**
   * 隐藏动作面板
   * @param {Object} e 
   */
  hideActionSheet: function(e) {
    const type = e.currentTarget.dataset.sheetType
    this.setData({
      [type]: false
    })
  },

  /**
   * 确认货物已退到门店
   */
  goConfirmReceipt: function() {
    const { order, btn } = this.data
    const str = JSON.stringify({ order, btn })
    wx.navigateTo({
      url: '/pages/regimental-commander/confirm-receipt/confirm-receipt?params=' + encodeURIComponent(str)
    })
  },

  /**
   * 撤销确认退货到店
   */
  revokeReceipt: function() {
    const _this = this
    wx.showModal({
      title: '您已“确认退货到店”，要撤销此操作吗？',
      content: '商品不在门店，才可以撒销',
      cancelText: "不撤销了",
      cancelColor: "#ee0a24",
      confirmText: "确认撤销",
      confirmColor:"#A6A6A6",
      complete: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中...',
          })
          const { btn, order } = _this.data
          const params = {
            order_refund_id: order.id
          }
          const url = app.getNetAddresss(btn.api)
          app._postNetWork({
            url,
            data: params,
            success: function(res) {
              const resData = res.data
              if (resData.result == 1) {
                wx.showModal({
                  title: '成功',
                  content: resData.msg,
                  showCancel: false,
                  confirmText: '好的',
                  confirmColor: '#ee0a24',
                  complete: (res) => {
                    if (res.confirm) {
                      wx.navigateTo({
                        url: '/pages/regimental-commander/order/order?orderType=waitRefund',
                      })
                    }
                  }
                })
              } else {
                wx.showToast({
                  icon: 'none',
                  title: resData.msg,
                })
              }
            },
            complete: function() {
              wx.hideLoading()
            }
          })
        }
      }
    })
  },

  /**
   * 获取售后的处理记录
   */
  getRefundRecord: function() {
    const refund_id = this.data.order.id
    let urlStr = app.getNetAddresss("refund.detail.process-log");
    const _this = this
    wx.showLoading({
      title: '加载中...',
    })
    app._postNetWork({
      url: urlStr,
      data: {refund_id},
      success: function(res) {
        const resData = res.data
        if (resData.result == 1) {
          const list = resData.data.map(item => {
            return {
              text: item.operate_name,
              desc: item.created_at,
              detail: item.detail
            }
          })
          // 数据需要反转
          _this.setData({ refundRecordList: list.reverse()})
        } else {
          wx.showToast({
            title: resData.msg,
          })
        }
      },
      complete: function() {
        wx.hideLoading()
      }
    })
  },

  /**
   * 复制
   * @param {Object} e 
   */
  copy: function(e) {
    const content = String(e.currentTarget.dataset.copyContent)
    wx.setClipboardData({
      data: content,
    })
  },

  /**
   * 拨打电话
   * @param {Object} e 
   */
  call: function(e) {
    const tel = String(e.currentTarget.dataset.tel)
    wx.makePhoneCall({
      phoneNumber: tel
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const params = JSON.parse(decodeURIComponent(options.params))
    this.setData({ order: params.order, btn: params.btn })
    this.getRefundRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})